package com.aqie.easy.heap;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 1046 最后一块石头的总量
 * poll是队列数据结构实现类的方法，从队首获取元素，同时获取的这个元素将从原队列删除；
 */
public class LastStoneWeight {
    public static int lastStoneWeight(int[] stones) {
        // PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        Queue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return (o2 - o1);
            }
        });

        for (int i = 0; i < stones.length; i++){
            maxHeap.offer(stones[i]);
        }


        while (maxHeap.size() > 1){

            int out = maxHeap.poll();
            int top =  maxHeap.poll();
            System.out.println(out + " " + top);

            if (out > top) maxHeap.add(out - top);
        }

        if (maxHeap.size() > 0) return maxHeap.peek();
        else return 0;

    }

    public static void main(String[] args) {
        int[] arr = {2,7,8,4,1,1};
        int i = lastStoneWeight(arr);
        System.out.println(i);
    }
}
